package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Admin;
import com.gk.carshopsys.pojo.Cust;
import com.gk.carshopsys.pojo.Emp;
import com.gk.carshopsys.service.EmpService;
import com.gk.carshopsys.service.EmpServiceImpl;
import com.gk.carshopsys.utils.EmpDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {
    @Autowired
    private EmpService empService;



    //Excel参数
    private HashMap<String,String> param;



    //excel传参
    @PostMapping("/emplist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }

    //excel下载
    @Log("导出员工")
    @GetMapping("/emplist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
        //导出的参数

        String ename = param.get("ename");
        String empno = param.get("empno");
        String job = param.get("job");
        String hiredate = param.get("hiredate");
        String dname = param.get("dname");

        Emp emp = new Emp();


            emp.setEmpno(empno);
            emp.setEname(ename);
            emp.setJob(job);
            emp.setDname(dname);

            ExcelUtils.export2Web(response, "员工表", "员工信息",Emp.class,empService.findEmp(emp));
        return "success";


    }


    // easyexcel上传文件
    @Log("导入员工")
    @PostMapping("/emplist/excelimport")
    public String upload(MultipartFile file,RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), Emp.class, new EmpDataListener(empService)).sheet().doRead();
        return "redirect:/emplist";
    }


    //跳转员工管理页面
    @RequestMapping("/emplist")
    public String toemplist(){
        return "emplist";
    }

    //员工查询
    @Log("查询员工")
    @ResponseBody
    @RequestMapping("/emplist/query")
    public List<Emp> findemp(@RequestBody  HashMap<String,String> params){
        String ename = params.get("ename");
        String empno = params.get("empno");
        String job = params.get("job");
        String hiredate = params.get("hiredate");
        String dname = params.get("dname");

        Emp emp = new Emp();


        if (hiredate!=null && !hiredate.equals("")){

//            Date date = Date.valueOf(hiredate);
            emp.setHiredate(hiredate);
            emp.setEmpno(empno);
            emp.setEname(ename);
            emp.setJob(job);
            emp.setDname(dname);
            return empService.findEmp(emp);
        }else {
            emp.setEmpno(empno);
            emp.setEname(ename);
            emp.setJob(job);
            emp.setDname(dname);
            return empService.findEmp(emp);
        }


    }

    //添加员工
    @Log("添加员工")
    @PostMapping("/emplist/add")
    public  String addcust(@RequestParam("add_empno") String empno,
                           @RequestParam("add_ename") String ename,
                           @RequestParam("add_sex") String sex,
                           @RequestParam("add_job") String job,
                           @RequestParam("add_hiredate") String hiredate,
                           @RequestParam("add_dname") String dname,
                           @RequestParam("add_telphone") String telphone,
                           @RequestParam("add_sal") Integer sal,
                           @RequestParam("add_state") String state,
                           RedirectAttributesModelMap model){

            Emp add_emp =new Emp(empno,ename,sex,job,hiredate,dname,telphone,sal,state,"888888");
            int result = empService.addEmp(add_emp);
            if(result==-1){
                model.addFlashAttribute("addmsg","添加员工失败，工号已存在");
                return "redirect:/emplist";
            }
            if (result==1){
                model.addFlashAttribute("addmsg","添加员工成功");
                return "redirect:/emplist";
            }else {
                model.addFlashAttribute("addmsg","添加员工失败，未知错误");
                return "redirect:/emplist";
            }


    }

    //更新员工
    @Log("更新员工")
    @PostMapping("/emplist/update")
    public  String upateemp(@RequestParam("update_id") Integer id,
                            @RequestParam("update_empno") String empno,
                            @RequestParam("update_ename") String ename,
                            @RequestParam("update_sex") String sex,
                            @RequestParam("update_job") String job,
                            @RequestParam("update_password") String password,
                            @RequestParam("update_hiredate") String hiredate,
                            @RequestParam("update_dname") String dname,
                            @RequestParam("update_telphone") String telphone,
                            @RequestParam("update_sal") Integer sal,
                            @RequestParam("update_state") String state,
                            RedirectAttributesModelMap model){

        Emp update_emp =new Emp(id,empno,ename,sex,job,hiredate,dname,telphone,sal,state,password);
        int result = empService.updateEmp(update_emp);

        if(result==-1){
            model.addFlashAttribute("updatemsg","修改员工失败，工号已存在");
            return "redirect:/emplist";
        }
        if (result==1){
            model.addFlashAttribute("updatemsg","修改员工成功");
            return "redirect:/emplist";
        }else {
            model.addFlashAttribute("updatemsg","修改员工失败，未知错误");
            return "redirect:/emplist";
        }


    }

    //批量删除员工
    @Log("删除员工")
    @ResponseBody
    @RequestMapping("/emplist/del")
    public Map<String,Integer> emplistdel(@RequestBody List<Integer> ids){
        int result = empService.delEmp(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }


    //登录页响应页面跳转
    @PostMapping ("/emplogin.action")
    public String adminlogin(@RequestParam("username") String username,
                             @RequestParam("password") String password, Model model, HttpSession session){


        Emp emp=empService.loginemp(username,password);
        if(emp!=null){
            session.setAttribute("loginUser",emp);
            session.setAttribute("username",emp.getEmpno());
            return "redirect:/empindex";
        }else{
            model.addAttribute("msg", "密码或者账号有误,请重新输入");
            return "login";
        }
    }

    //主页跳转
    @RequestMapping("/empindex")
    public String toindex(){return "emp/empindex";}


    //个人信息修改跳转
    @RequestMapping("/empinformation")
    public String toempinformation(){

        return  "emp/empinformation";
    }

    //员工个人信息更新
    @Log("修改个人信息")
    @PostMapping("/emplist/updateone")
    public  String upateoneemp(@RequestParam("update_id") Integer id,
                            @RequestParam("update_empno") String empno,
                            @RequestParam("update_ename") String ename,
                            @RequestParam("update_sex") String sex,
                            @RequestParam("update_job") String job,
                            @RequestParam("update_password") String password,
                            @RequestParam("update_hiredate") String hiredate,
                            @RequestParam("update_dname") String dname,
                            @RequestParam("update_telphone") String telphone,
                            @RequestParam("update_sal") Integer sal,
                            @RequestParam("update_state") String state,
                            RedirectAttributesModelMap model,HttpSession session){

        Emp update_emp =new Emp(id,empno,ename,sex,job,hiredate,dname,telphone,sal,state,password);
        int result = empService.updateEmp(update_emp);

        if (result==1){
            model.addFlashAttribute("updatemsg","修改个人信息成功");
            Emp emp=empService.loginemp(empno,password);
            session.setAttribute("username",emp.getEmpno());
            session.setAttribute("loginUser",emp);
            return "redirect:/empinformation";
        }else {
            model.addFlashAttribute("updatemsg","修改个人信息失败，未知错误");
            return "redirect:/empinformation";
        }
    }

    @RequestMapping("/emppassword")
    public String toemppassword(){

        return  "emp/emppassword";
    }

    //员工个人密码修改
    @Log("修改密码")
    @PostMapping("/emplist/updatepassword")
    public String updatepassword(@RequestParam("password") String password,
                                 @RequestParam("new_password") String new_password,
                                 @RequestParam("cheack_password") String cheack_password,
                                 @RequestParam("update_id") Integer id,
                                 @RequestParam("update_empno") String update_empno,
                                 RedirectAttributesModelMap model, HttpSession session){

        if (new_password.equals(cheack_password)){
            Emp emp = empService.loginemp(update_empno,password);
            if (emp!=null){
                int result=empService.changePassword(id,new_password);
                if(result!=1){
                    model.addFlashAttribute("msg","修改密码未知错误，请联系管理员");
                    return "redirect:/emppassword";
                }
                model.addFlashAttribute("msg","修改成功，请重新登陆");
                //session.invalidate();
                return "redirect:/emppassword";

            }else {
                model.addFlashAttribute("msg","密码错误，请重新输入");
                return "redirect:/emppassword";
            }
        }else {
            model.addFlashAttribute("msg","两次输入密码不一致");
            return "redirect:/emppassword";
        }


    }

}
